package cn.wizzer.app.web.modules.controllers.crown.order;

import cn.wizzer.app.crown.modules.models.CrownOrder;
import cn.wizzer.app.crown.modules.models.enums.SettleStatusEnum;
import cn.wizzer.app.crown.modules.services.order.CrownOrderService;
import cn.wizzer.app.sys.modules.services.SysParamService;
import cn.wizzer.app.web.commons.filter.ApiTokenFilter;
import cn.wizzer.app.web.commons.ulog.annotation.ULog;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.base.model.enums.BizExceptionEnum;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.*;

import java.math.BigDecimal;

/**
 * 投资、记录Controller
 *
 * @author
 */
@At("/api/match/hg/order")
@IocBean
public class ApiCrownOrderController {
    @Reference(retries = 0, timeout = 5000)
    @Inject
    private CrownOrderService orderService;

    @Reference
    @Inject
    private SysParamService sysParamService;

    /**
     * 投资
     *
     * @param order
     * @return
     */
    @At("/create")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    @ULog(tag = "用户投资创建", msg = "投资金额: ${order.amount}")
    public Result create(@Param("..") CrownOrder order) {
        String appid = Mvcs.getReq().getHeader("appid");
        if (null == order || null == order.getAmount()) {
            return Result.error(BizExceptionEnum.RATE_ERROR_BET_FAILED);
        }
        // 判断金额小于等于0
        if (order.getAmount().compareTo(BigDecimal.ZERO) < 1) {
            return Result.error(BizExceptionEnum.MONEY_ERROR);
        }
        orderService.create(appid, order);
        return Result.success();
    }


    /**
     * @param orderType  订单类型
     * @param startTime  开始时间戳
     * @param endTime    结束时间戳
     * @param pageNumber 页码
     * @param pageSize   容量
     * @return Result
     */
    @At("/list")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result list(@ParamVerify(required = true) SettleStatusEnum orderType,
                       @Param("startTime") Long startTime,
                       @Param("endTime") Long endTime,
                       @ParamVerify(required = true) int pageNumber,
                       @ParamVerify(required = true) int pageSize) {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success(orderService.list(appid, orderType, startTime, endTime, pageNumber, pageSize));
    }


    /**
     * 服务费占比
     *
     * @return
     */
    @At("/serviceFee")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result serviceFee() {
        return Result.success(sysParamService.fetch().getServiceFee());
    }


    /**
     * 用户投资、收益信息
     *
     * @return
     */
    @At
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result info() {
        return Result.success(orderService.info(Mvcs.getReq().getHeader("appid")));
    }


    /**
     * 投资详情
     *
     * @param id 投资id
     * @return
     */
    @At
    @Ok("json:full")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result detail(@ParamVerify(required = true) String id) {
        return Result.success(orderService.detail(id));
    }


    /**
     * 撤销投资
     *
     * @param id
     * @return
     */
    @At("/cancel")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    @ULog(tag = "用户撤销投资", msg = "订单id: ${id}")
    public Result cancel(@ParamVerify(required = true) String id) {
        orderService.cancel(id);
        return Result.success();
    }


    /**
     * 用户盈亏报表
     *
     * @param timezone
     * @param startTime
     * @param endTime
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @At("/userProfit")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result userProfit(@Param("timezone") String timezone,
                             @Param("startTime") Long startTime,
                             @Param("endTime") Long endTime,
                             @ParamVerify(required = true) int pageNumber,
                             @ParamVerify(required = true) int pageSize) {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success().addData(orderService.userProfit(appid, timezone, startTime, endTime, pageNumber, pageSize));
    }

}





